package com.laicunba.ext.web.controller.console;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.laicunba.ext.web.controller.BaseController;
import com.laicunba.pojo.TemplateDO;
import com.laicunba.service.TemplateService;
import com.laicunba.util.CacheUtil;
import com.laicunba.util.DateUtil;
import com.laicunba.util.StringUtil;

@Controller
@RequestMapping(value = "/console/ext")
public class SkinProjectController extends BaseController {
	@Autowired
	private TemplateService templateService;

	@RequestMapping(value = "skin_project.htm")
	public ModelAndView skinProject() {
		ModelAndView view = new ModelAndView("skin_project.jsp");
		view.addObject("iosSkinList", templateService.findByType("ios_skin"));
		view.addObject("androidSkinList", templateService.findByType("android_skin"));

		view.addObject("iosTestSkin", CacheUtil.hgetAll("sys.app.skin.IOS_SKIN.test"));
		view.addObject("iosProductSkin", CacheUtil.hgetAll("sys.app.skin.IOS_SKIN.product"));
		view.addObject("androidTestSkin", CacheUtil.hgetAll("sys.app.skin.ANDROID_SKIN.test"));
		view.addObject("androidProductSkin", CacheUtil.hgetAll("sys.app.skin.ANDROID_SKIN.product"));
		return view;
	}

	@RequestMapping(value = "skin_form.htm")
	public ModelAndView skinForm(String id) {
		ModelAndView view = new ModelAndView("skin_form.jsp");
		view.addObject("skin", templateService.get(TemplateDO.class, id));
		return view;
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "skin_save.htm")
	public String skinSave(String id, String content) {
		TemplateDO tempDO = templateService.get(TemplateDO.class, id);
		tempDO.setContent(content);
		JSONObject json = new JSONObject();
		json.put("code", templateService.updateSkipCheckOwner(tempDO));
		return json.toJSONString();
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "skin_publish.htm")
	public String skinPublish(String id, String beginDate, String endDate, String type, int version) {
		TemplateDO tempDO = templateService.get(TemplateDO.class, id);
		JSONObject json = new JSONObject();
		if (tempDO == null) {
			json.put("code", "SKIN_TEMPLATE_DO_NOT_EXIST");
			return json.toJSONString();
		}
		Date bDate = null;
		Date eDate = null;
		try {
			bDate = StringUtil.isNotEmpty(beginDate) ? DateUtil.parse(beginDate) : new Date();
			bDate = bDate == null ? new Date() : bDate;
			eDate = StringUtil.isNotEmpty(endDate) ? DateUtil.parse(endDate) : new Date();
			if (eDate == null) {
				eDate = new Date(bDate.getTime());
				eDate.setHours(eDate.getHours() + 2);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		String key = "sys.app.skin." + ("ios_skin".equalsIgnoreCase(tempDO.getType()) ? "IOS_SKIN" : "ANDROID_SKIN") + "." + ("test".equalsIgnoreCase(type) ? "test" : "product");
		Map<String, String> val = new HashMap();
		val.put("begin_date", bDate.getTime() + "");
		val.put("end_date", bDate.getTime() + "");
		val.put("version", version + "");
		val.put("content", tempDO.getContent() == null ? "" : tempDO.getContent());
		val.put("code", tempDO.getCode());
		CacheUtil.hmset(key, val);
		json.put("code", "SUCCESS");
		return json.toJSONString();
	}
}
